//
//  YMBridging.m
//  YMMedical
//
//  Created by y s on 2023/6/19.
//

#import "YMBridging.h"
#import "YMMyModel.h"
#import "YMLoginViewController.h"
#import "YMNavigationController.h"
#import "YMForgetPwdViewController.h"
#import "YMDeviceScanQRCodeVC.h"
#import "AppDelegate.h"
#import "YMBaseTabBarController.h"
#import "YMHomeTipInforAlert.h"
#import "YMHealthTestReportVC.h"
#import "YMChangePwdViewController.h"
#import "YMProtocolViewController.h"
#import "YMWebBaseVC.h"
#import "YMFeedbackViewController.h"
#import "YMTestUserViewController.h"
#import "YMProblemViewController.h"
#import "YMProductSpecificationVC.h"
#import "YMMyDeviceViewController.h"
#import "YMToolChooseStatusSheet.h"


@implementation YMBridging

//跳登录
+ (BOOL)goLogin{
    if([YMMyModel isLogin]){
        return NO;
    }
    [self goToLogin];
    return YES;
}
//跳转到登录
+ (void)goToLogin {
    YMLoginViewController *vc = [[YMLoginViewController alloc] init];
    YMNavigationController *nav = [[YMNavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [SJumpUtil presentVC:nav animated:YES];
}
//忘记密码
+ (void)goToForgetPwdVC{
    YMForgetPwdViewController *vc = [[YMForgetPwdViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}
//修改密码
+ (void)goToChangePwdVC{
    YMChangePwdViewController *vc = [[YMChangePwdViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}
/** 1:用户协议 2:隐私政策 */
+ (void)goToProtocolVC:(NSInteger)type{
    YMProtocolViewController *vc = [[YMProtocolViewController alloc] init];
    vc.protocolType = type;
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}
// 呼叫电话
+ (void)callPhone:(NSString *)phone{
    if(phone.length < 1){
        return;
    }
    phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
    if ([iOSVersion compare:@"10.2" options:NSNumericSearch] == NSOrderedDescending || [iOSVersion compare:@"10.2" options:NSNumericSearch] == NSOrderedSame){
#if TARGET_IPHONE_SIMULATOR
        [SHUD showInfo:[NSString stringWithFormat:@"模拟器不能拨打电话[%@]",phone]];
#else
        NSString *phoneUrl = [NSString stringWithFormat:@"tel://%@",phone];
        [SJumpUtil openURL:phoneUrl];
#endif
        return;
    }
    UIViewController *current = [SJumpUtil rootVC];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:phone message: nil preferredStyle:UIAlertControllerStyleAlert];
    // 设置popover指向的item
    alert.popoverPresentationController.barButtonItem = current.navigationItem.leftBarButtonItem;
    // 添加按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"点击了呼叫按钮10.2下");
        NSString *phoneUrl = [NSString stringWithFormat:@"tel://%@",phone];
        UIApplication *app = [UIApplication sharedApplication];
        if ([app canOpenURL:[NSURL URLWithString:phoneUrl]]) {
            [SJumpUtil openURL:phoneUrl];
        }
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:KLanguage(@"cancle_txt") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];
    [current presentViewController:alert animated:YES completion:nil];
}
//H5
+ (void)goToWebVC:(NSString *)url
            title:(NSString *)title
     navBarHidden:(BOOL)navBarHidden{
    YMWebBaseVC *vc = [[YMWebBaseVC alloc] init];
    vc.webUrl = url;
    vc.strTitle = title;
    vc.hideNav = navBarHidden;
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}

//扫描二维码
+ (void)gotoScanCodeVC:(BOOL)backStep
                isCard:(BOOL)isCard
           finishBlock:(void(^)(NSString *valueStr))finishBlock{
    if ([YMBridging checkCameraVideoStatus]){
        YMDeviceScanQRCodeVC *vc = [[YMDeviceScanQRCodeVC alloc] init];
        vc.backStep = backStep;
        vc.isCard = isCard;
        vc.finishBlock = ^(NSString * _Nonnull valueStr) {
            if (finishBlock){
                finishBlock(valueStr);
            }
        };
        vc.hidesBottomBarWhenPushed = YES;
        [SJumpUtil pushVC:vc animated:YES];
    }
}
//app home 页面
+ (void)goAppTabWithIndex:(NSInteger)index{
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    YMBaseTabBarController *tabVc = (YMBaseTabBarController *)app.window.rootViewController;
    [SJumpUtil.topVC.navigationController popToRootViewControllerAnimated:YES];
    [tabVc setSelectedIndex:index];
}
//回到首页 有alert
+ (void)goHomeTabWithAlert{
    YMHomeTipInforAlert *alert = [[YMHomeTipInforAlert alloc] init];
    alert.iconName = @"alert_tips_infor_icon";
    alert.strContent = KLanguage(@"dete_not_save_txt");
    alert.sureBlock = ^{
        [YMBridging goAppTabWithIndex:0];
    };
    [alert show];
}
//检测报告详情
+ (void)goToHealthTestReportVC:(BOOL)backHome
                   detectionId:(NSString *)idStr
                        target:(UIViewController *)target{
    YMHealthTestReportVC *vc = [[YMHealthTestReportVC alloc] init];
    vc.backHome = backHome;
    vc.detectionId = idStr;
    vc.hidesBottomBarWhenPushed = YES;
    if(target == nil){
        [SJumpUtil pushVC:vc animated:YES];
    }else{
        [target.navigationController pushViewController:vc animated:YES];
    }
    
}

+ (void)goToFeedBackVC{
    YMFeedbackViewController *vc = [[YMFeedbackViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}
//被测试人管理
+ (void)goToTestUserVC{
    YMTestUserViewController *vc = [[YMTestUserViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}
//常见问题
+ (void)goToProblemViewController{
    YMProblemViewController *vc = [[YMProblemViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}
//产品说明
+ (void)goToProductSpecificationVC{
    YMProductSpecificationVC *vc = [[YMProductSpecificationVC alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}
//我的设备
+ (void)goToMyDeviceViewController{
    YMMyDeviceViewController *vc = [[YMMyDeviceViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}



/** 分享 */
+ (void)goToShareWithTitle:(NSString *)title
                       url:(NSString *)url
                     image:(UIImage *)image
                   success:(void(^)(void))success{
    dispatch_async(dispatch_get_main_queue(), ^{
        // 异常的代码 放置如下位置 异步添加进任务队列，添加一个子线程，去刷新UI界面。
        //设置分享的内容
        NSMutableArray *arrAc = [[NSMutableArray alloc] init];
        if (title != nil && title.length > 0){
            [arrAc addObject:title];
        }
        if (url != nil && url.length > 0){
            [arrAc addObject:[NSURL URLWithString:url]];
        }
        if (image != nil){
            [arrAc addObject:image];
        }
         
        UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:arrAc applicationActivities:nil];
        //很多系统自带的分享功能如果不需要的可以隐去，即设置不出现在活动项目中的选项数组
        activity.excludedActivityTypes = @[UIActivityTypePrint,
                                           UIActivityTypeCopyToPasteboard,
                                           UIActivityTypeAssignToContact
                                           ,UIActivityTypeSaveToCameraRoll];
        [SJumpUtil.topVC presentViewController:activity animated:YES completion:nil];
        activity.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
               if (completed) {
                   if (success){
                       success();
                   }
                   NSLog(@"分享成功");
               }else{
                   NSLog(@"分享取消");
               }
           };
    });
}
//检查相机是否可用
+ (BOOL)checkCameraVideoStatus{
    NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        NSString *errorStr = @"应用相机权限受限,请在iPhone的“设置-隐私-相机”选项中，允许访问你的相机。";
        YMHomeTipInforAlert *alert = [[YMHomeTipInforAlert alloc] init];
        alert.iconName = @"alert_tips_infor_icon";
        alert.strContent = errorStr;
        alert.sureBlock = ^{
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
                
            }];
        };
        [alert show];
        return NO;
    }else{
        return YES;
    }
}

//切换语言
+ (NSString *)KLanguage:(NSString *)key{
    if (key != nil && key.length > 0){
        return KLanguage(key);
    }
    return @"";
}
//语言选择框
+ (void)showLanguageSheet:(NSString *)chooseStr
                sureBlock:(void(^)(NSString *text))sureBlock{
    NSArray *arrTemp = @[KLanguage(@"language_system"),
                         KLanguage(@"language_chinese"),
                         KLanguage(@"language_english")];
    YMToolChooseStatusSheet *vSheet = [[YMToolChooseStatusSheet alloc] init];
    vSheet.strTitle = KLanguage(@"language_txt");
    vSheet.nowSelect = chooseStr;
    vSheet.arrData = arrTemp;
    vSheet.textSureBlock = ^(NSString * _Nonnull text, NSInteger index) {
        [YMLanguageUtil setLanguagueWithType:index];
        [[NSNotificationCenter defaultCenter] postNotificationName:kLanguage_change object:nil];
        if (sureBlock) {
            sureBlock(text);
        }
    };
    [vSheet show];
}
/** 获取设置的语言名称*/
+ (NSString *)getSettingLanguage{
    return [YMLanguageUtil getSettingLanguage];
}

@end
